<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <!-- <van-cell v-for="item in list" :key="item" :title="item" /> -->
      <!-- 评论列表组件分离 -->
      <article-item
        v-for="(item,index) in list"
        :key="index"
        :item="item"
        @replyBtn="$emit('replyBtn',$event)"
      />
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->

    <!-- /发布评论 -->
  </div>
</template>
           
<script>
import articleItem from "@/components/article/article-comm-item";
import { getComments_api } from "@/api/article/index";
export default {
  components: {
    articleItem
  },
  props: {
    source: {
      type: [Number, String, Object],
      required: true
    },
    list: {
      type: [Array, Object],
      default: () => [] //默认是空数组，这里要用函数的写法
    },
    type: {
      //发送请求的参数   a、c
      type: String,
      //自定义prop 数据验证
      validator(val) {
        return ["a", "c"].includes(val);
      },
      default: "a"
    }
  },
  data() {
    return {
      //   list: [],
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null, //获取下一页数据的标记
      limit: 10,
      total: 0
    };
  },
  created() {
    this.loading=true
    this.onLoad();
  },
  methods: {
    /*
       获取文章的评论和评论回复是同一个接口
       唯一的区别是接口参数不一样
       type
          a 文章的评论
          c 评论的回复
       source 
           文章的评论传递文章的id
           评论的回复，传递评论的id   
    */
    async onLoad() {
      this.loading = true;
      try {
        const { data: res } = await getComments_api({
          type: this.type, //评论类型，a-对文章(article)的评论，c-对评论(comment)的回复
          source: this.source.toString(), //源id，文章id或评论id
          offset: this.offset, //获取评论数据偏移量，值为评论id，表示从此id的数据向后取，不传表示从第一页开始读取数
          limit: this.limit
        });
        // console.log(res, "tttttt");
        this.loading = false;
        this.total = res.data.total_count;
        this.$emit("total", this.total);
        // this.list = [...this.list, ...res.data.results];
        this.list.push(...res.data.results);
        if (res.data.results.length) {
          this.offset = res.data.last_id;
        } else {
          this.finished = true;
        }
      } catch (error) {
        this.$toast("获取失败");
        this.finished = true;
      }
    }
  }
};
</script>
<style scoped lang="less">
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}

.van-list {
  margin-bottom: 300px;
}
</style>